#!/usr/bin/env pwsh
# 测试用户行为监控修复效果

Write-Host "=== 用户行为监控修复验证 ===" -ForegroundColor Green

# 1. 检查前端修复
Write-Host "`n1. 检查前端 showContactRecord 方法修复..." -ForegroundColor Yellow
$vueFile = "d:/src/DotNet/Lzfy_Refund_Service/RefundAuditWeb/src/views/RefundTaskManagement.vue"

if (Test-Path $vueFile) {
    $vueContent = Get-Content $vueFile -Raw
    
    # 检查是否先调用 startSession 再调用 trackViewContact
    if ($vueContent -match "const sessionId = await userBehaviorService\.startSession") {
        Write-Host "✓ 前端调用顺序已修复：先 startSession 后 trackViewContact" -ForegroundColor Green
    } else {
        Write-Host "✗ 前端调用顺序未正确修复" -ForegroundColor Red
    }
    
    # 检查是否添加了延迟
    if ($vueContent -match "setTimeout.*100") {
        Write-Host "✓ 已添加延迟确保会话建立" -ForegroundColor Green
    } else {
        Write-Host "✗ 未添加延迟机制" -ForegroundColor Red
    }
    
    # 检查是否添加了错误处理
    if ($vueContent -match "try.*catch.*error") {
        Write-Host "✓ 已添加错误处理" -ForegroundColor Green
    } else {
        Write-Host "✗ 未添加错误处理" -ForegroundColor Red
    }
} else {
    Write-Host "✗ Vue 文件不存在" -ForegroundColor Red
}

# 2. 检查后端修复
Write-Host "`n2. 检查后端 UpdateOperationSessionAsync 方法修复..." -ForegroundColor Yellow
$serviceFile = "d:/src/DotNet/Lzfy_Refund_Service/Services/UserBehaviorService.cs"

if (Test-Path $serviceFile) {
    $serviceContent = Get-Content $serviceFile -Raw
    
    # 检查是否添加了重试机制
    if ($serviceContent -match "int retryCount = 0") {
        Write-Host "✓ 后端已添加重试机制" -ForegroundColor Green
    } else {
        Write-Host "✗ 后端未添加重试机制" -ForegroundColor Red
    }
    
    # 检查是否添加了延迟
    if ($serviceContent -match "Task\.Delay") {
        Write-Host "✓ 后端已添加重试延迟" -ForegroundColor Green
    } else {
        Write-Host "✗ 后端未添加重试延迟" -ForegroundColor Red
    }
    
    # 检查是否添加了日志
    if ($serviceContent -match "LogWarning.*会话.*重试") {
        Write-Host "✓ 后端已添加重试日志" -ForegroundColor Green
    } else {
        Write-Host "✗ 后端未添加重试日志" -ForegroundColor Red
    }
} else {
    Write-Host "✗ Service 文件不存在" -ForegroundColor Red
}

# 3. 检查关键配置
Write-Host "`n3. 检查关键配置..." -ForegroundColor Yellow

# 检查 ActionTypes 常量
$modelsFile = "d:/src/DotNet/Lzfy_Refund_Service/Models/UserBehaviorModels.cs"
if (Test-Path $modelsFile) {
    $modelsContent = Get-Content $modelsFile -Raw
    if ($modelsContent -match "VIEW_PHONE") {
        Write-Host "✓ VIEW_PHONE 常量已定义" -ForegroundColor Green
    } else {
        Write-Host "✗ VIEW_PHONE 常量未定义" -ForegroundColor Red
    }
} else {
    Write-Host "✗ Models 文件不存在" -ForegroundColor Red
}

# 4. 生成修复总结
Write-Host "`n=== 修复总结 ===" -ForegroundColor Green
Write-Host "主要修复内容：" -ForegroundColor White
Write-Host "1. 前端调用顺序：先建立会话(startSession)，再跟踪行为(trackViewContact)" -ForegroundColor White
Write-Host "2. 添加延迟机制：确保会话完全建立后再进行行为跟踪" -ForegroundColor White
Write-Host "3. 后端重试机制：处理会话可能尚未建立的并发情况" -ForegroundColor White
Write-Host "4. 错误处理：添加完善的错误处理和日志记录" -ForegroundColor White

Write-Host "`n预期效果：" -ForegroundColor White
Write-Host "- phone_view_time 字段将正确记录查看电话的时间" -ForegroundColor White
Write-Host "- first_input_time、last_input_time 将正确记录输入时间" -ForegroundColor White
Write-Host "- save_time 将正确记录保存时间" -ForegroundColor White
Write-Host "- total_duration、phone_to_save_duration、input_duration 将正确计算" -ForegroundColor White
Write-Host "- input_char_count、contact_result、user_notes 将正确记录" -ForegroundColor White
Write-Host "- is_completed 将正确设置为 1" -ForegroundColor White
Write-Host "- risk_score、risk_factors 将正确计算和记录" -ForegroundColor White

Write-Host "`n建议测试步骤：" -ForegroundColor Yellow
Write-Host "1. 重新编译并启动后端服务" -ForegroundColor White
Write-Host "2. 刷新前端页面" -ForegroundColor White
Write-Host "3. 执行完整的联系流程：查看详情 -> 点击联系 -> 输入备注 -> 选择结果 -> 保存" -ForegroundColor White
Write-Host "4. 检查数据库 lzfy_user_operation_session 表中的记录" -ForegroundColor White

Write-Host "`n=== 验证完成 ===" -ForegroundColor Green